package com.zdy.zdycouponview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/**
 * Created by zdy on 2016/11/22.
 */

public class ZdyCouponView extends LinearLayout {
    private Paint paint;
    private Paint paint1;
    private int radiusBackgroundColor = Color.BLACK;
    private int radiusBackgroundColor1 = Color.YELLOW;
    private int radiusBig = 30;
    private int radiusSmall = 8;
    private int gap = 15;
    private int hNums;
    private Context context;

    public ZdyCouponView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        radiusBackgroundColor1 = Color.parseColor("#f5f5f5");
        radiusBig = Dp2Px(context, 5);
        radiusSmall = Dp2Px(context, 2);
        gap = Dp2Px(context, 2);
        paint1 = new Paint();
        paint = new Paint();
        paint.setColor(radiusBackgroundColor);
        paint1.setColor(radiusBackgroundColor1);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
    }

    public ZdyCouponView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ZdyCouponView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(0, radiusBig, radiusBig, paint1);
        canvas.drawCircle(getWidth(), radiusBig, radiusBig, paint1);
        canvas.drawCircle(0, radiusBig, radiusBig, paint);
        canvas.drawCircle(getWidth(), radiusBig, radiusBig, paint);
        for (int i = 0; i < hNums; i++) {
            canvas.drawCircle(gap + radiusSmall + radiusBig + i * (2 * radiusSmall + gap), radiusBig, radiusSmall, paint);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        hNums = (getWidth() - radiusBig * 2) / (2 * radiusSmall + gap);
    }

    public int Dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
}
